//
//  DUMineViewController.m
//  toutiao
//
//  Created by duyujin1 on 2021/7/27.
//

#import "DUMineViewController.h"
#import "Masonry.h"
#import "DULogin.h"
#import <SDWebImage.h>
#import "DUAlert.h"
#import "DULanguageController.h"

@interface DUMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *tableViewHeaderView;
@property (nonatomic,strong) UIImageView *headerImageView;
@property (nonatomic,strong) UILabel *headerLabel;
@end

@implementation DUMineViewController

-(instancetype)init{
    if(self=[super init]){
        NSString * title=NSLocalizedString(@"mine", nil);
        self.tabBarItem.title=title;
        self.tabBarItem.image=[UIImage imageNamed:@"icon.bundle/home@2x.png"];
        self.tabBarItem.selectedImage=[UIImage imageNamed:@"icon.bundle/home_selected@2x.png"];
    }
    return self;
}

-(void)dealloc{
    //[self removeObserver:self forKeyPath:@"refreshMineView"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshMineView:) name:@"refreshMineView" object:nil];
}

-(void)refreshMineView:(NSNotification *)notif{
    [self.tableView reloadData];
}

#pragma mark- tableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 2;
    }
    else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"mineCell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mineCell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return cell;
}

#pragma mark- tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;{
    if(section==0){
        return self.tableViewHeaderView;
    }else{
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 200;
    }else{
        return 0;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    if (![[DULogin sharedLogin] isLogin]) {
        [_headerImageView setImage:[UIImage imageNamed:@"icon.bundle/icon.png"]];
        [_headerLabel setText:NSLocalizedString(@"Please click the image to log in", nil)];
    }else{
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:[DULogin sharedLogin].avatarUrl]];
        [self.headerLabel setText:NSLocalizedString(@"Click the image to log out", nil)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section==0){
        cell.userInteractionEnabled=NO;
        if (indexPath.row == 0) {
            cell.textLabel.text = [[DULogin sharedLogin] isLogin] ? [DULogin sharedLogin].nick: NSLocalizedString(@"nickname",nil);
        }
        else {
            cell.textLabel.text = [[DULogin sharedLogin] isLogin] ? [DULogin sharedLogin].address: NSLocalizedString(@"region",nil);
        }
    }else{
        cell.textLabel.text =NSLocalizedString(@"language setting",nil);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        return;
    }else{
        //语言设置界面
        DULanguageController *vc = [DULanguageController new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark- privateMethods

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}

-(UIView *)tableViewHeaderView{
    if(!_tableViewHeaderView){
        _tableViewHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
        _tableViewHeaderView.backgroundColor = [UIColor whiteColor];
        [_tableViewHeaderView addSubview:self.headerImageView];
        [_tableViewHeaderView addSubview:self.headerLabel];
        [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.tableViewHeaderView);
            make.top.equalTo(self.tableViewHeaderView).offset(20);
            make.width.height.mas_equalTo(140);
        }];
        [self.headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.headerImageView);
            make.bottom.equalTo(self.tableViewHeaderView).offset(-10);
        }];
    }
    return _tableViewHeaderView;
}

-(UIImageView *)headerImageView{
    if(!_headerImageView){
        _headerImageView=[[UIImageView alloc] initWithFrame:CGRectZero];
        _headerImageView.backgroundColor=[UIColor whiteColor];
        _headerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _headerImageView.clipsToBounds = YES;
        _headerImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tapGuesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapImage)];
        [_headerImageView addGestureRecognizer:tapGuesture];
    }
    return _headerImageView;
}

-(UILabel *)headerLabel{
    if(!_headerLabel){
        _headerLabel=[[UILabel alloc] initWithFrame:CGRectZero];
        _headerLabel.font=[UIFont systemFontOfSize:14];
        _headerLabel.textColor=[UIColor lightGrayColor];
    }
    return _headerLabel;
}

- (void)_tapImage {
    __weak typeof(self) weakSelf = self;
    if (![[DULogin sharedLogin] isLogin]) {
        //没有登录的时候拉起登录
        [[DULogin sharedLogin] loginWithFinishBlock:^(BOOL isLogin) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            //SDK流程之后判断是否登录成功
            if (isLogin) {
                [strongSelf.tableView reloadData];
            }
        }];
    }else{
        [DUAlert showAlertWithTitle:@"确定退出？" detailIfon:@"退出后某些功能将不可用" confirmHandle:^{
            //已经登录的退出登录
            [[DULogin sharedLogin] logOut];
            [self.tableView reloadData];
        } cancelHandle:^{
            
        }];
    }
}

@end
